﻿//导航配置数据
var globalNav = [{
	"id": "0",
	"name": "首页",
	"icon": "iconfont icon-shouye",
	"href": "home.html?nav=0"
}, {
	"id": "1",
	"name": "投资",
	"icon": "iconfont icon-touzi",
	"href": "investment.html?nav=1"
}, {
	"id": "2",
	"name": "进度",
	"icon": "iconfont icon-jindukongzhi",
	"href": "progress.html?nav=2"
}, {
	"id": "3",
	"name": "监控",
	"icon": "iconfont icon-jiankong",
	"href": "monitoring.html?nav=3"
}, {
	"id": "4",
	"name": "全景",
	"icon": "iconfont icon-yulan",
	"href": "panorama.html?nav=4"
}, {
	"id": "5",
	"name": "评价",
	"icon": "iconfont icon-xing",
	"href": "evaluation.html?nav=5"
}];

mui.ready(function() {
	$('#global-nav-box').on('tap', 'a', function() {
		var id = $(this).data("id");
	})
	var navBox = $('#global-nav-box');
	$.each(globalNav, function(i, item) {
		var itemId = item.id;
		var actId = GetQueryString('nav') || "0";
		tpl = '<li class="' + (itemId == actId ? 'mui-active' : '') + '"><a href="javascript:;" data-href="' + item.href + '" data-id="' + itemId + '"><i class="' + item.icon + '"></i>' + item.name + '</a></li>';
		navBox.append(tpl);
	});
	//设置用户头像，姓名
	var useravatar = localStorage.getItem("userAvatar");
	var username = localStorage.getItem("userName");
	$('.avatar-box').html('<img src="' + useravatar + '" class="avatar"/> <span class = "user-name" > ' + username + ' </span>');

	$('a[data-href]').on('tap', function() {
		var href = $(this).data('href');
		if(href == 'none') {
			mui.toast('开发中');
			return false;
		}
		mui.openWindow({
			url: href,
			id: href
		})
	})
})
window.GetQueryString = function(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if(r != null) return decodeURI(r[2]);
	return null;
}
window.getClientHeight = function() {
	var h = document.documentElement.clientHeight;
	return h;

}
window.loading = {
	show: function() {
		var tpl = $('<div class="globaLoading"><div class="text-box">加载中</div></div>');

		$('body').append(tpl);
	},
	hide: function() {
		$('.globaLoading').hide();
	}

}
//随机数生成
window.randomNum = function(minNum, maxNum) {
	switch(arguments.length) {
		case 1:
			return parseInt(Math.random() * minNum + 1, 10);
			break;
		case 2:
			return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
			break;
		default:
			return 0;
			break;
	}
}
//缓存用户数据
window.saveUserInfo = function() {
	localStorage.setItem('userAvatar', $('#headimgurl').val() || "assets/img/avatar.jpg");
	localStorage.setItem('userName', $('#nickname').val() || "匿名");
}